/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.common.Constants;
import com.youth.fund.hk.hostManager.manager.datamodel.RecruitmentModel;
import com.youth.fund.hk.hostManager.manager.service.RecruitmentService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月10日下午5:33:18
 */
@Controller
@RequestMapping("/recruitment")
public class RecruitmentController
{
	@Autowired
	@Qualifier("RecruitmentService")
	private RecruitmentService service;

	@RequiresPermissions("recruitment:view")
	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest req, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		RecruitmentModel rm = new RecruitmentModel();

		rm.setLang(lang);

		model.addAttribute("recruitmentList", this.service.query(rm));

		return "recruitment/list";
	}

	@RequiresPermissions("recruitment:view")
	@RequestMapping(value = "/{language}", method = RequestMethod.GET)
	public String listLang(@PathVariable("language") String language,
			HttpServletRequest req, Model model)
	{
		String lang = language;

		req.getSession().setAttribute("local_lang", lang);

		RecruitmentModel rm = new RecruitmentModel();

		rm.setLang(lang);

		model.addAttribute("recruitmentList", this.service.query(rm));

		return "recruitment/list";
	}

	@RequiresPermissions("recruitment:create")
	@RequestMapping(value = "/create", method = { RequestMethod.GET })
	public String showCreateForm(HttpServletRequest req, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}
		setCommonData(model, lang);

		RecruitmentModel rm = new RecruitmentModel();

		rm.setLang(lang);

		model.addAttribute("recruitment", rm);

		model.addAttribute("op", "新增");

		return "recruitment/edit";
	}

	@RequiresPermissions("recruitment:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(HttpServletRequest req, RecruitmentModel model,
			RedirectAttributes redictAttributes)
	{
		boolean result = service.create(model);

		if (result)
		{
			redictAttributes.addFlashAttribute("msg", "新增成功");
		} else
		{
			redictAttributes.addFlashAttribute("msg", "新增失败");
		}

		return "redirect:/recruitment";
	}

	@RequiresPermissions("recruitment:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(HttpServletRequest req,
			@PathVariable("id") int id, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		RecruitmentModel condition = new RecruitmentModel();

		condition.setId(id);

		condition.setLang(lang);

		model.addAttribute("recruitment", service.queryOne(condition));

		model.addAttribute("op", "修改");

		return "recruitment/edit";
	}

	@RequiresPermissions("recruitment:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(RecruitmentModel model,
			RedirectAttributes redirectAttribute)
	{
		boolean result = service.update(model);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "修改成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "修改失败");
		}

		return "redirect:/recruitment";
	}

	@RequiresPermissions("recruitment:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String showDeleteForm(HttpServletRequest req,@PathVariable("id") int id, Model model)
	{
		String lang;

		if (req.getSession().getAttribute("local_lang") != null)
		{
			lang = req.getSession().getAttribute("local_lang").toString();
		} else
		{
			lang = Constants.LANG_ZH_HK;

			req.getSession().setAttribute("local_lang", lang);
		}

		setCommonData(model, lang);

		RecruitmentModel condition = new RecruitmentModel();

		condition.setId(id);
		
		condition.setLang(lang);

		model.addAttribute("recruitment", service.queryOne(condition));

		model.addAttribute("op", "删除");

		return "recruitment/edit";
	}

	@RequiresPermissions("recruitment:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	public String delete(@PathVariable("id") int id,
			RedirectAttributes redirectAttribute)
	{
		RecruitmentModel condition = new RecruitmentModel();

		condition.setId(id);

		boolean result = service.delete(condition);

		if (result)
		{
			redirectAttribute.addFlashAttribute("msg", "删除成功");
		} else
		{
			redirectAttribute.addFlashAttribute("msg", "删除失败");
		}

		return "redirect:/recruitment";
	}

	private void setCommonData(Model model, String lang)
	{
		RecruitmentModel rm = new RecruitmentModel();

		rm.setLang(lang);

		model.addAttribute("recruitmentList", this.service.query(rm));
	}
}
